﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using ExpenseSystem.Common;
using ExpenseSystem.Model;
using ExpenseSystem.Common.BaseModel;
using ExpenseSystem.Lor;

namespace ExpenseSystem.Core.Converter
{
    public class RestainConverter
    {
        public static Restrain[] FromRequestArgs(RequestArgs arg)
        {
            if (arg == null||arg.Args==null)
                return new Restrain[] { };
            Restrain[] restrains = new Restrain[arg.Args.Count()];
            int i = 0;
            foreach(RequestArg a in arg.Args)
            {
                restrains[i] = FromRequestArg(a);
                i++;
            }
            return restrains;
        }
        public static Restrain FromRequestArg(RequestArg arg)
        {
            if (arg == null)
            {
                LogHelper.Info(typeof(RestainConverter).Name,"传入了空的参数");
                return null;
            }
            if(arg.Op==null)
            {
                return null;
            }
            switch(arg.Op.ToUpper())
            {
                case "LK":
                    return Restrain.Lk(arg.Key, arg.Value);
                case "EQ":
                    return Restrain.Eq(arg.Key,arg.Value);
                case "GE":
                    return Restrain.Ge(arg.Key, arg.Value);
                case "GT":
                    return Restrain.Gt(arg.Key, arg.Value);
                case "IN":
                    return Restrain.In<String>(arg.Key, (String[])arg.Value);
                case "LE":
                    return Restrain.Le(arg.Key, arg.Value);
                case "LT":
                    return Restrain.Lt(arg.Key, arg.Value);
                case "NE":
                    return Restrain.NotEq(arg.Key, arg.Value);
                case "NI":
                    return Restrain.NotIn(arg.Key, arg.Value);
                default:
                    return null;
            }
        }
    }
}
